package com.yuanfeng.userms.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.userms.entity.UserLiveStatisticsEntity;
import com.yuanfeng.userms.mapper.UserLiveStatisticsMapper;
import com.yuanfeng.userms.service.UserLiveStatisticsService;
import com.yuanfeng.userms.vo.MemberMessageListVO;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserLiveStatisticsServiceImpl extends ServiceImpl<UserLiveStatisticsMapper, UserLiveStatisticsEntity>
    implements UserLiveStatisticsService{

    @Override
    public ResponseResult saveLiveStatistics(Map<String, Object> map) {
        UserLiveStatisticsEntity liveStatisticsEntity = JSON.parseObject(JSONObject.toJSONString(map), UserLiveStatisticsEntity.class);
        String todayTime = DateUtils.getDate();

        int count=this.baseMapper.selectCount(new LambdaQueryWrapper<UserLiveStatisticsEntity>()
                .eq(UserLiveStatisticsEntity::getUserId,liveStatisticsEntity.getUserId())
                .eq(UserLiveStatisticsEntity::getPlatForm,liveStatisticsEntity.getPlatForm())
                .apply("DATE_FORMAT (login_time,'%Y-%m-%d')",todayTime));

        if (count == 0) {
            int  result = this.baseMapper.insert(liveStatisticsEntity);
            return result== CommonStatusEnum.SUCCESS.getCode()?ResponseResult.success():ResponseResult.fail();
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryUserNikkatsuDetailPort(Map<String, Object> param) {

        IPage<MemberMessageListVO> page=this.baseMapper.queryUserNikkatsuDetailPort(new Query<MemberMessageListVO>().getPage(param),
                param);
        for (MemberMessageListVO result : page.getRecords()) {
            if (null != result.getUserRegTime()) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Long time = new Long(result.getUserRegTime());
                String d = format.format(time * 1000);
                result.setUserRegTime(d);
            }
            if(null!=result.getUserMobile()&&!("".equals(result.getUserMobile()))){
                String  hidenPhone= result.getUserMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
                result.setUserMobile(hidenPhone);
            }
        }
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult queryUserNikkatsuPort(Map<String, Object> param) {
        List<UserLiveStatisticsEntity> list=this.baseMapper.queryUserNikkatsuPort(param);
        return ResponseResult.success(list);
    }
}




